using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wicresoft.OmsClient
{
    public partial class Send : Form
    {
        public Send()
        {
            InitializeComponent();
        }


        private void Send_Load(object sender, EventArgs e)
        {
            this.txtUrl.Text = SyncConfigManage.ProviderUrl;
            CheckInput();
        }

        private void btnTo_Click(object sender, EventArgs e)
        {
            try
            {
                AddressBook frmab = new AddressBook();
                frmab.TO = this.txtTos.Text;
                frmab.ShowDialog();
                if (frmab.IsOK)
                    this.txtTos.Text = frmab.TO;
            }
            catch (Exception ex)
            {
                Logger.Exception("Send.btnTo_Click ", ex);
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                if (StateLight.CurrentLight == "Gray")
                {
                    MessageBox.Show("Service unavailable", "Error");
                    return;
                }
                if (!UserService.IsAuthorized)
                {
                    MessageBox.Show("You are unauthorized, please check your CIL, password or contact your administrator", "Warnning");
                    return;
                }
                this.btnSend.Enabled = false;
                this.btnTo.Enabled = false;
                this.txtTos.Enabled = false;
                this.txtContent.Enabled = false;

                this.timerSending.Enabled = true;

                SendSMSAsync send = new SendSMSAsync(BeginSend);
                send.BeginInvoke(this.txtTos.Text.Trim(), this.txtContent.Text.Trim(), null, null);
                    
            }
            catch (Exception ex)
            {
                Logger.Exception("Send.btnSend_Click ", ex);
            }
        }

        private delegate void SendSMSAsync(string to, string content);

        private void BeginSend(string to, string content)
        {
            string result = SendSMS.Send(to, content);
            if (result != "Unable to connect to the remote server")
                if (CheckResult(result))
                {
                    AfterSend(true);
                }
                else
                {
                    MessageBox.Show("Send Messages Failed", "Failed");
                    AfterSend(false);
                }
            else
            {
                MessageBox.Show("Unable to connect to the remote server", "Failed");
                AfterSend(false);
            }
        }

        private delegate void SendComplate(bool isClose);

        private void AfterSend(bool isClose)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SendComplate(AfterSend), isClose);
            }
            else
            {
                this.timerSending.Enabled = false;
                this.Text = "Send SMS";
                this.btnSend.Enabled = true;
                this.btnTo.Enabled = true;
                this.txtTos.Enabled = true;
                this.txtContent.Enabled = true;
                if (isClose)
                    this.Close();
            }
        }

        private void CheckInput()
        {
            if (this.txtContent.Text.Trim() == "" || this.txtTos.Text.Trim() == "")
            {
                this.btnSend.Enabled = false;
            }
            else
                this.btnSend.Enabled = true;
        }

        private void txtTos_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        private void txtContent_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        private bool CheckResult(string result)
        {
            if (result.Contains("code=\"ok\""))
                return true;
            else
                return false;
        }

        private void timerSending_Tick(object sender, EventArgs e)
        {
            string title = this.Text;
            if (title.Length == 8)
                title = "Send SMS (Sending)";
            else if (title.Length == 18)
                title = "Send SMS (Sending.)";
            else if (title.Length == 19)
                title = "Send SMS (Sending..)";
            else if (title.Length == 20)
                title = "Send SMS (Sending...)";
            else if (title.Length == 21)
                title = "Send SMS (Sending....)";
            else if (title.Length == 22)
                title = "Send SMS (Sending.....)";
            else if (title.Length == 23)
                title = "Send SMS (Sending......)";
            else if (title.Length == 24)
                title = "Send SMS (Sending)";
            this.Text = title;
        }
    }
}